//
//  HomePageViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomePageViewController.h"
#import "ButtonViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "HomePage.h"
#import "HomePageCell.h"
#import "UIImageView+WebCache.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HomePageViewController ()
@property(nonatomic,retain)UITableView *scrollTableView;
@property(nonatomic,retain)NSMutableArray *modelArr;
@end

@implementation HomePageViewController
- (void)dealloc
{
    [_modelArr release];
    [_scrollTableView release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
    }
    [self dataScroll];
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addButtonView];
    // Do any additional setup after loading the view.
    self.scrollTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, WIDTH, HEIGHT-80) style:UITableViewStylePlain];
    [self.view addSubview:self.scrollTableView];
    [_scrollTableView release];
    self.scrollTableView.rowHeight = 150;
    self.scrollTableView.dataSource = self;
    self.scrollTableView.delegate = self;
    
    
    
}
// 首页遮挡的View
- (void)addButtonView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width/4, 50)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    [view release];
    view.hidden = YES;
    if (self.colorNum == 1) {
        view.hidden = NO;
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/4, 50)];
    [view addSubview:label];
    [label release];
    label.text = @"首页";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:38/255.0 green:100/255.0 blue:200/255.0 alpha:1];
    label.font = [UIFont systemFontOfSize:15];
}

// 必需实现的两种方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    HomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[HomePageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    HomePage *home = self.modelArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:home.pic_url];
    [cell.imageViewOne sd_setImageWithURL:url];
    return cell;
    
    
}

// 首页数据解析
- (void)dataScroll
{
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:@"http://online.dongting.com/recomm/recomm_modules?&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=5644dc238845c5739a5e37f08f22c51d938fc544&idfa=4331AEED-1B57-45FB-87C9-D9421ECD697F&utdid=VLoWmt5itKgDAPTY+A6pV5QN&alf=201200&version=1429261203" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dataDic = dic[@"data"];
        NSMutableArray *postersArr = dataDic[@"posters"];
        NSMutableDictionary *postersDic = [postersArr lastObject];
        NSMutableArray *dataArr = postersDic[@"data"];
        for (NSInteger i = 0; i < dataArr.count; i++) {
            HomePage *home = [[HomePage alloc] init];
            NSMutableDictionary *dic = dataArr[i];
            [home setValuesForKeysWithDictionary:dic];
            NSLog(@"%@",home.pic_url);
            [self.modelArr addObject:home];
        }
        [self.scrollTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"NO");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
